package com.woniuxy.java102vuecli.configuration;

import com.woniuxy.java102vuecli.interceptor.AuthorizationInterceptor;
import com.woniuxy.java102vuecli.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author author
 * @Create 2023-09-13 16:42
 */
@Configuration
public class CorsConfiguration {

    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    AuthorizationInterceptor authorizationInterceptor;

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")//配置允许跨域的请求
                        .allowedOrigins("*")      //配置允许跨域的域名
                        .allowedHeaders("*")      //配置允许跨域携带请求头信息
                        .allowedMethods("*")        //配置允许跨域的请求类型 POST GET DELETE PUT      Restful
                        .exposedHeaders("*");       //暴露响应头
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //拦截器执行属性和配置属性一致
                registry.addInterceptor(loginInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/employee/login", "/verifycode", "/verifycode1", "/email/**", "/image/**","/sms/**"/*, "/images/**"*/);
                registry.addInterceptor(authorizationInterceptor)
                        .addPathPatterns("/**")
                        .excludePathPatterns("/employee/login", "/verifycode", "/verifycode1", "/email/**", "/image/**", "/sms/**",/*"/images/**",*/
                                "/student/**", "/department/**", "/role/**","/menu/**"  );
            }

            //配置映射路径转换
/*            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/images/**").addResourceLocations("file:///d:/images/");
            }*/
        };
    }
}
